package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.vo.QuestionUserRelVO;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneLikesQuestion;
import com.baizhi.shane.shane.service.ShaneLikesQuestionService;
import com.baizhi.shane.shane.mapper.ShaneLikesQuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wbz12
* @description 针对表【shane_likes_question(啥呢问题点赞表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
public class ShaneLikesQuestionServiceImpl extends ServiceImpl<ShaneLikesQuestionMapper, ShaneLikesQuestion>
    implements ShaneLikesQuestionService{

    @Resource
    private CommonUtils commonUtils;
    @Resource
    private RedisManager redisManager;

    /**
     * 用户点赞问题，只需要questionId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 18:33 2023/10/4
     * @Param questionUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String insertOneQuestionLikes(QuestionUserRelVO questionUserRelVO) {
        Long userId = redisManager.getUserId();
        Long questionId = questionUserRelVO.getQuestionId();
        ShaneLikesQuestion shaneLikesQuestion = new ShaneLikesQuestion();
        shaneLikesQuestion.setUserId(userId);
        shaneLikesQuestion.setQuestionId(questionId);
        boolean save = this.save(shaneLikesQuestion);
        String succeed = commonUtils.boolToString(save, SysMapping.LIKES_SUCCESS, SysMapping.LIKES_FAIL);
        return succeed;
    }

    /**
     * 用户取消点赞问题，只需要questionId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 18:48 2023/10/4
     * @Param questionUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String deleteOneQuestionLikes(QuestionUserRelVO questionUserRelVO) {
        Long userId = redisManager.getUserId();
        Long questionId = questionUserRelVO.getQuestionId();

        LambdaUpdateWrapper<ShaneLikesQuestion> uwSlq = new LambdaUpdateWrapper<>();
        uwSlq.eq(ShaneLikesQuestion::getUserId,userId);
        uwSlq.eq(ShaneLikesQuestion::getQuestionId,questionId);
        uwSlq.eq(ShaneLikesQuestion::getDelFlag,0);
        uwSlq.set(ShaneLikesQuestion::getDelFlag, 1);
        uwSlq.set(ShaneLikesQuestion::getDelBy, userId);
        uwSlq.set(ShaneLikesQuestion::getDelTime, new Date());
        boolean update = this.update(uwSlq);
        String succeed = commonUtils.boolToString(update, SysMapping.LIKES_CANCEL_SUCCESS, SysMapping.LIKES_CANCEL_FAIL);
        return succeed;
    }
}




